package com.example.online_order.conrtoller;

import com.example.online_order.mapper.GoodsMapper;
import com.example.online_order.pojo.Evalate;
import com.example.online_order.pojo.Goods;
import com.example.online_order.pojo.Result;
import com.example.online_order.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
@CrossOrigin
@Slf4j
public class GoodsController {

//    查询（菜名名称模糊查询） 增删改
    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsMapper goodsMapper;

    @PostMapping("/addGoods")
    public Result addGoods(@RequestBody Goods goods){
        Integer res = goodsService.addGood(goods);
        if (res != null ){
            return new Result(goods);
        }else{
            return  Result.fail("evalation already",500);
        }
    }

    @DeleteMapping("/delGoods/{id}")
    public Result delGoods(@PathVariable("id") Integer id){
        Integer res = goodsService.deleteGood(id);
        if (res != null ){
            return new Result(id);
        }else{
            return  Result.fail("delete failed",444);
        }
    }

    @PutMapping("/updateGoods")
    public Result updateGoods(@RequestBody Goods goods){
        Integer res = goodsService.updateGood(goods);
        if (res != null ){
            return new Result("update success");
        } else{
            return  Result.fail("update failed",444);
        }
    }

    @GetMapping("/getAllGoods")
    public Result getAllGoods(){
        return new Result(goodsService.selectAllGoods());
    }

    @GetMapping("/getMealType")
    public Result getMealType(){ return new Result(goodsService.selectMealType());}

    @GetMapping("/like/mealName/{mealName}/mealType/{mealType}")
    public Result selectGoods(@PathVariable("mealName") String mealName, @PathVariable("mealType") String mealType){
        List<Goods> goods = goodsService.selectDsql(mealName,mealType);
        if (goods.isEmpty()){
            return Result.fail("该查询结果为空",404);
        } else {
            return Result.success(goods);
        }
    }
}
